package sgi.client;

import java.awt.image.BufferedImage;
import java.io.IOException;

import javax.swing.JOptionPane;

import sgi.message.MessageImageAdd;

public class SGIImageAddController extends SGIController {

	private static SGIImageAddController ImageAdd;
	private static SGIImageAddUI ImageAddUI;	
	
	/** constructor */
	private SGIImageAddController() {
		ImageAddUI = new SGIImageAddUI();
	}
	/** message to add the new image with all the details to image database table */
	public void update(String Date,String Location, String Latitude, 
			String Longitude,byte[] originalImage1, byte[] originalImage2,
			byte[] originalImage3, byte[] originalImage4, byte[] originalImage5)
	{
		MessageImageAdd msg = new MessageImageAdd(Date,Location,Latitude,Longitude,originalImage1,
				originalImage2,originalImage3, originalImage4, originalImage5);
		try {
			SGIClientConnectionController.createConnection().handleMessageFromClientUI(msg);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	/** creating the controller */
	public static SGIImageAddController create() throws IOException
	{
		if(SGIImageAddController.ImageAdd == null)
		{
			SGIImageAddController.ImageAdd = new SGIImageAddController();
			ImageAddUI.setSGIImageAddController(SGIImageAddController.ImageAdd);
		}	
		SGIImageAddController.ImageAddUI.setVisible(true);
		return ImageAdd;
	}
	
	/** terminating the UI */
	public static void terminate()
	{
		SGIImageAddController.ImageAdd = null;
		SGIImageAddController.ImageAddUI.setSGIImageAddController(null);
		SGIImageAddController.ImageAddUI.setVisible(false);
		SGIImageAddController.ImageAddUI = null;
	}
	/** if we did not insert all the fields of the image table */
	public void FieldNotInserted()
	{
		JOptionPane.showMessageDialog(SGIImageAddController.getUI(),"not all fields filled");
	}
	/** handle message to inform that images have been uploaded */
	@Override
	void handleMessage(Object msg) {
		ImageAddUI.notify("Images have been uploaded.");

	}
	/** getting the imageadd UI */
	public static SGIImageAddUI getUI()
	{
		return ImageAddUI;
	}

}
